import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('../views/layout.vue'),
      children: [
        {
          path: '/',
          name: '菜品信息',
          meta: { show: true },
          component: () => import('../views/homeView.vue'),
        },
        {
          path: '/dishDate',
          name: '菜品安排',
          meta: { show: 'admin' },
          component: () => import('../views/dishDateView.vue'),
        },
        {
          path: '/manage',
          name: '菜品管理',
          meta: { show: 'admin' },
          component: () => import('../views/dishManageView.vue'),
        },
        {
          path: '/userManage',
          name: '用户管理',
          meta: { show: 'admin' },
          component: () => import('../views/userManageView.vue'),
        },
        {
          path: '/userCenter',
          name: '个人中心',
          meta: { show: true },
          component: () => import('../views/userCenterView.vue')
        },
        {
          path: '/userDateManageView',
          name: '员工订单总览',
          meta: { show: 'admin' },
          component: () => import('../views/userDateManageView.vue')
        },
        {
          path: '/noticeView',
          name: '公告管理',
          meta: { show: 'admin' },
          component: () => import('../views/noticeView.vue')
        },
        {
          path: '/adviceView',
          name: '意见反馈',
          meta: { show: true },
          component: () => import('../views/adviceView.vue')
        },
        {
          path: '/adviceManageView.vue',
          name: '意见管理',
          meta: { show: 'admin' },
          component: () => import('../views/adviceManageView.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/loginView.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'notFound',
      component: () => import('../views/notFound.vue'),
    }
  ],
})

import { useUserStore } from '@/stores'
router.beforeEach((to, from, next) => {
  const useUser = useUserStore()
  // 获取token
  const token = useUser.token
  if (to.path === '/login') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
